<?php

/**
 * Description of SecureController
 *
 * @author gun
 */
class Admin_SecureController extends Zend_Controller_Action {
    
    public static $_NS = 'Web_App_Signin';
    
    public function init() {
        $session = new Zend_Session_Namespace(self::$_NS);
        
        if (!isset($session->user) || (isset($session->user) && !is_object($session->user)))
            $this->redirect('admin/login/index');
        
        $totalroles = $session->user->total_roles;

        $url = $this->_helper->AdminUrl($this->getRequest()->getControllerName() . "/" . $this->getRequest()->getActionName());     
        $fault = 0;

        /**
         * IF requested url not matched with url in database,
         * user will redirected to admin/index
         */
        foreach ($session->user->roles as $key => $value) {
            if (false === strpos($url, ($value ? $value : '##/##'))) {
                $fault = $fault + 1;
            }
        }
//        echo $fault, ' : ', $totalroles, exit;
        
        if ($fault === $totalroles) $this->redirect('admin/index');
    }
}
